from selenium import webdriver
from time import sleep,time
from selenium.webdriver.support.select import Select
import random


class AddProject:

    def addproject(self,username='admin',password='123456'):
        try:
            driver = webdriver.Chrome()
            driver.get('http://localhost/ranzhi/www/sys/user-login.html')
            driver.maximize_window()
            # 隐式等待
            driver.implicitly_wait(10)

            # 登陆
            driver.find_element_by_id('account').send_keys(username)
            driver.find_element_by_id('password').send_keys(password)
            driver.find_element_by_id('submit').click()

            # 点击 项目
            driver.find_element_by_id('s-menu-3').click()

            # 定位iframe元素
            iframe = driver.find_element_by_id('iframe-3')
            # 切换到iframe中
            driver.switch_to.frame(iframe)

            for i in range(2,5):
                # 点击 添加区块
                driver.find_element_by_partial_link_text('添加区块').click()

                # 选择区块
                select = driver.find_element_by_id('blocks')
                blocks = ['task','project']
                block = random.choice(blocks)
                Select(select).select_by_value(block)

                # 区块名称
                driver.find_element_by_id('title').send_keys('block%d'%i)

                # 宽度
                select = driver.find_element_by_id('grid')
                Select(select).select_by_index(random.randint(0,5))

                # 颜色
                driver.find_element_by_xpath('//*[@id="ajaxForm"]/table/tbody/tr[2]/td/div/div/div/button').click()
                driver.find_element_by_xpath('//*[@id="ajaxForm"]/table/tbody/tr[2]/td/div/div/div/div/li[%d]/button'%random.randint(1,6)).click()

                # 任务 - 任务列表独有
                if block == 'task':
                    driver.find_element_by_xpath('//*[@id="paramstype_chosen"]/a/span').click()
                    driver.find_element_by_xpath('//*[@id="paramstype_chosen"]/div/ul/li[%d]'%random.randint(1,5)).click()
                elif block == 'project':
                    driver.find_element_by_xpath('//*[@id="paramsstatus_chosen"]/a/span').click()
                    driver.find_element_by_xpath('//*[@id="paramsstatus_chosen"]/div/ul/li[%d]'%random.randint(1,4)).click()

                # 数量
                element = driver.find_element_by_id('params[num]')
                element.clear()
                element.send_keys(random.randint(10,50))

                # 排序
                driver.find_element_by_xpath('//*[@id="paramsorderBy_chosen"]/a/span').click()
                driver.find_element_by_xpath('//*[@id="paramsorderBy_chosen"]/div/ul/li[%d]'%random.randint(1,6)).click()

                # 任务状态
                if block == 'task':
                    driver.find_element_by_xpath('//*[@id="paramsstatus_chosen"]/ul').click()
                    driver.find_element_by_xpath('//*[@id="paramsstatus_chosen"]/div/ul/li[%d]'%random.randint(1,6)).click()
            
                # 保存
                driver.find_element_by_id('submit').click()

                sleep(2)

        # except Exception as e:
        #     print(e)
        finally:
            driver.close()

if __name__ == "__main__":
    add = AddProject()
    add.addproject()